package com.jsbb.service.sys;

import com.jsbb.dto.sys.SysMenuQueryDto;
import com.jsbb.dto.sys.SysMenuSaveDto;
import com.jsbb.dto.sys.SysMenuUpdateDto;
import com.jsbb.exception.ServiceException;
import com.jsbb.mapper.sys.SysMenuMapper;
import com.jsbb.mapper.sys.SysUserMapper;
import com.jsbb.model.sys.SysMenu;
import com.jsbb.model.sys.SysUser;
import com.jsbb.util.IdWorkerUtils;
import com.jsbb.vo.sys.SysMenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 菜单Service
 *
 * @author w
 * @date 2021-03-05
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    public List<SysMenuVo> list(SysMenuQueryDto queryDto) {
        List<SysMenu> list = sysMenuMapper.selectByCondition(queryDto);
        List<SysMenuVo> voList = new ArrayList();
        for (SysMenu sysMenu : list) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            if (sysMenu.getParentId().equals(0L)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    public List<SysMenuVo> getChildren(List<SysMenu> list, Long id) {
        List<SysMenuVo> voList = new ArrayList();
        for (SysMenu sysMenu : list) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            if (sysMenu.getParentId() != null && sysMenu.getParentId().equals(id)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    public SysMenuVo get(Long id) {
        SysMenu sysMenu = sysMenuMapper.selectByPrimaryKey(id);
        if (sysMenu == null) {
            throw new ServiceException("数据不存在");
        }
        SysMenuVo sysMenuVo = new SysMenuVo();
        BeanUtils.copyProperties(sysMenu, sysMenuVo);
        return sysMenuVo;
    }

    public List<SysMenuVo> selectByUserId(Long userId) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(userId);
        if (sysUser == null) {
            return null;
        }
        List<SysMenu> list = null;
        if (sysUser.getType() == 0) {
            //内置用户拥有所有菜单权限、按钮权限
            list = sysMenuMapper.selectByCondition(null);
        } else {
            list = sysMenuMapper.selectByUserId(userId);
        }
        List<SysMenuVo> voList = new ArrayList();
        for (SysMenu sysMenu : list) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            if (sysMenu.getParentId().equals(0L)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    public void save(SysMenuSaveDto saveDto) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(saveDto, sysMenu);
        sysMenu.setId(IdWorkerUtils.idWorker.nextId());
        sysMenu.setParentId(saveDto.getParentId() == null ? 0L : saveDto.getParentId());
        sysMenu.setSort(saveDto.getSort() == null ? 0 : saveDto.getSort());
        sysMenu.setCreateTime(new Date());
        sysMenu.setUpdateTime(new Date());
        sysMenuMapper.insert(sysMenu);
    }

    public void update(SysMenuUpdateDto updateDto) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(updateDto, sysMenu);
        sysMenu.setParentId(updateDto.getParentId() == null ? 0L : updateDto.getParentId());
        sysMenu.setSort(updateDto.getSort() == null ? 0 : updateDto.getSort());
        sysMenu.setUpdateTime(new Date());
        sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
    }

    public void delete(Long id) {
        sysMenuMapper.deleteByPrimaryKey(id);
    }
}
